package com.sopaths.taglib;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class RequireJSTag extends SimpleTagSupport {

	private String dataMain;

	@Override
	public void doTag() throws JspException, IOException {
		PageContext context = (PageContext) getJspContext();
		HttpServletRequest request = (HttpServletRequest) context.getRequest();
		String contextPath = request.getContextPath();
		String configHtml = "<script type=\"text/javascript\" src=\"" + contextPath + "/resources/config.js\"></script>";
		String baseUrlConfig = "<script type=\"text/javascript\">require.baseUrl = \"" + contextPath + "/resources\";</script>";
		String requireHtml = "<script type=\"text/javascript\" data-main=\"" + contextPath + "/resources/js/" + dataMain + "\" src=\"" + contextPath + "/resources/js/lib/requirejs/require.js \"></script>";
		context.getOut().println(configHtml);
		context.getOut().println(baseUrlConfig);
		context.getOut().println(requireHtml);
	}

	public String getDataMain() {
		return dataMain;
	}

	public void setDataMain(String dataMain) {
		this.dataMain = dataMain;
	}

}
